/*  
 *  ------ [802_08] - get RSSI from last received packet  -------- 
 *  
 *  Explanation: This program shows how to get RSSI value from the 
 *  last received packet. In this protocol it is possible to extract this 
 *  information from each packet. Besides, there is an API function which
 *  shows this information. Before running this example, make sure there 
 *  is another emitter sending packets to this XBee module in order to 
 *  receive information.
 *
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <MakeSureXBee802.h>

// variable to store RSSI
int rssi;

uint8_t state=0;
uint8_t g=0;


void setup()
{  
	Serial.begin(9600);
	Serial.print("802_08 example");

  // init XBee 
  xbee802.ON(); 

}


void loop()
{

  // check available data in RX buffer
  if( xbee802.available() > 0 ) 
  {
    // parse information
    xbee802.treatData(); 

    // check RX flag after 'treatData'
    if( !xbee802.error_RX ) 
    {
      // check available packets
      while( xbee802.pos>0 )
      {  
        // get RSSI signal and make conversion to -dBm
        rssi=xbee802.packet_finished[xbee802.pos-1]->RSSI;
        rssi*=-1;
        
        Serial.print("RSSI(dBm): ");   
		Serial.print(rssi);

        // free memory
        free(xbee802.packet_finished[xbee802.pos-1]);
        xbee802.packet_finished[xbee802.pos-1]=NULL; 

        // decrement the received packet counter
        xbee802.pos--; 
      }
    } 
  }


  // Getting RSSI using the API function
  // This function returns the last received packet's RSSI
  xbee802.getRSSI();
  if( !xbee802.error_AT )
  {      
    Serial.print("getRSSI(dBm): "); 
    
    //get rssi from getRSSI function and make conversion
    rssi=xbee802.valueRSSI[0];
    rssi*=-1;  
	Serial.println(rssi,DEC);
  }
  Serial.println();
  
  
  delay(5000);
}






